﻿using Kavaa.Core.Data;
using Kavaa.Modules.PortailMedical.Entity;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Kavaa.Modules.PortailMedical.Datalayer
{
    internal static class AccreditationDAO
    {
        public static void AddAccreditation(int MedecinId, int CliniqueId, int ActeId)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_AddAccreditation]"
            , new List<SqlParameter>() { 
                new SqlParameter("@MedecinId", MedecinId),
                new SqlParameter("@CliniqueId", CliniqueId),
                new SqlParameter("@ActeId", ActeId),
                new SqlParameter("@Date", DateTime.Now),
                    });

        }

        public static Accreditations GetAccreditationsByUserByActe(int acteId, int userId)
        {
           Accreditations Accreditations = new Accreditations();

            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetAccreditationsByUserByActe]"
            , new List<SqlParameter>() { 
                new SqlParameter("@acteId", acteId),
                new SqlParameter("@userId", userId),
            }))
            {
                Accreditations.Add(new Accreditation()
                {
                    Id = (row["Id"] != DBNull.Value) ? Convert.ToInt32(row["Id"]) : 0,
                    Medecin = new Medecin() { Id= (row["IdMedecin"] != DBNull.Value) ? Convert.ToInt32(row["IdMedecin"]) : 0},
                    Clinique = new Clinique() { Id = (row["IdClinique"] != DBNull.Value) ? Convert.ToInt32(row["IdClinique"]) : 0 },
                    Acte =new Acte(){ Id=(row["IdActe"] != DBNull.Value) ? Convert.ToInt32(row["IdActe"]) : 0},
                    Etat_prestataire = (row["Etat_prestataire"] != DBNull.Value) ? Convert.ToInt32(row["Etat_prestataire"]) : 0,
                    Etat_Clinique = (row["Etat_Clinique"] != DBNull.Value) ? Convert.ToInt32(row["Etat_Clinique"]) : 0,
                
                });
            }

            return Accreditations;
        }

        public static Accreditations GetAccreditationsByUser(int userId)
        {
            Accreditations Accreditations = new Accreditations();


            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetAccreditationsByUser]"
            , new List<SqlParameter>() { 
                new SqlParameter("@userId", userId),
            }))
            {
                Accreditations.Add(new Accreditation()
                {
                    Clinique = new Clinique() { Name = (row["CliniqueName"] != DBNull.Value) ? Convert.ToString(row["CliniqueName"]) : null },
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Etat_Clinique = (row["EtatAccreditation"] != DBNull.Value) ? Convert.ToInt32(row["EtatAccreditation"]) : 0,
                    Date = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    DateAccredit = (row["DateAccreditation"] != DBNull.Value) ? Convert.ToDateTime(row["DateAccreditation"]) : DateTime.Now,

                });
            }

            return Accreditations;
        }

        public static Accreditations GetAccreditationsAttente(int cliniqueId)
        {
            Accreditations Accreditations = new Accreditations();


            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetAccreditationsAttente]"
            , new List<SqlParameter>() { 
                new SqlParameter("@cliniqueId", cliniqueId),
            }))
            {
                Accreditations.Add(new Accreditation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Date = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    Medecin = new Medecin()
                    {
                        Name = (row["nom"] != DBNull.Value) ? Convert.ToString(row["nom"]) : null,
                        Prenom = (row["prenom"] != DBNull.Value) ? Convert.ToString(row["prenom"]) : null,
                        INP = (row["INP"] != DBNull.Value) ? Convert.ToString(row["INP"]) : null
                    },

                });
            }

            return Accreditations;
        }

        public static void UpdateEtatAccredit(int etat, int id)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_UpdateEtatAccredit]"
            , new List<SqlParameter>() { 
                new SqlParameter("@etat", etat),
                new SqlParameter("@ID", id)
              
               
            });

        }
        public static Accreditations GetAccreditationsAttentePrestataire()
        {
            Accreditations Accreditations = new Accreditations();


            foreach (var row in SqlDaoAdapter.ExecuteReader(
                ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_GetAccreditationsAttentePrestataire]"
          ))
            {
                Accreditations.Add(new Accreditation()
                {
                    Id = (row["ID"] != DBNull.Value) ? Convert.ToInt32(row["ID"]) : 0,
                    Acte = new Acte() { Name = (row["ActeName"] != DBNull.Value) ? Convert.ToString(row["ActeName"]) : null },
                    Date = (row["DateDemande"] != DBNull.Value) ? Convert.ToDateTime(row["DateDemande"]) : DateTime.Now,
                    Medecin = new Medecin()
                    {
                        Name = (row["nom"] != DBNull.Value) ? Convert.ToString(row["nom"]) : null,
                        Prenom = (row["prenom"] != DBNull.Value) ? Convert.ToString(row["prenom"]) : null,
                        INP = (row["INP"] != DBNull.Value) ? Convert.ToString(row["INP"]) : null
                    },

                });
            }

            return Accreditations;
        }

        public static void UpdateEtatAccreditPrestataire(int etat, int id)
        {

            SqlDaoAdapter.ExecuteNonQuery(ConfigurationCache.DatabaseConnectionString,
                "[dbo].[sp_PortailMedical_UpdateEtatAccreditPrestataire]"
            , new List<SqlParameter>() { 
                new SqlParameter("@etat", etat),
                new SqlParameter("@ID", id)
              
               
            });

        }
    }
}
